package com.sunny.ihealth.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sunny.ihealth.BaseActivity;
import com.sunny.ihealth.R;
import com.sunny.ihealth.entity.bmob.UserInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMTextMessage;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.core.BmobIMClient;
import cn.bmob.newim.event.MessageEvent;
import cn.bmob.newim.listener.ConversationListener;
import cn.bmob.newim.listener.MessageListHandler;
import cn.bmob.newim.listener.MessageSendListener;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;

public class InviteYourFriendActivity extends BaseActivity implements View.OnClickListener, MessageListHandler {

    private static final String TAG = "InviteYourFriendA";
    private TextView tvText;
    private Button btnInvite, btnCancel;

    BmobIMUserInfo user;
    private UserInfo userInfo;
    private UserInfo me;
    private BmobIMConversation c;
    private BmobIMTextMessage message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_your_friend);

        initViews();
        initData();
    }

    @Override
    public void initViews() {
        setToolbar("邀请");
        setEnableBackIcon(true);
        setStatusBarColor();

        tvText = (TextView) findViewById(R.id.tv_inviteYourFriend_text);
        btnInvite = (Button) findViewById(R.id.btn_inviteYourFriend_invite);
        btnCancel = (Button) findViewById(R.id.btn_inviteYourFriend_cancel);

        btnInvite.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

        if ((userInfo = (UserInfo) getIntent().getSerializableExtra("data")) == null) {
            Toast.makeText(InviteYourFriendActivity.this, "没有数据...", Toast.LENGTH_SHORT).show();
            return;
        }

        tvText.setText(String.format(Locale.getDefault(), "邀请%s一起跑步", userInfo.getNickname()));


    }

    @Override
    public void initData() {
        user = new BmobIMUserInfo();
        user.setUserId(userInfo.getUsername());
        user.setName(userInfo.getNickname());
        BmobIM.getInstance().startPrivateConversation(user, false, new ConversationListener() {
            @Override
            public void done(BmobIMConversation c, BmobException e) {
                if (e != null) {
                    Log.e(TAG, "连接错误:   " + e.getMessage());
                } else {
                    InviteYourFriendActivity.this.c = BmobIMConversation.obtain(BmobIMClient.getInstance(), c);
                }
            }
        });

        me = BmobUser.getCurrentUser(getApplicationContext(), UserInfo.class);
        message = new BmobIMTextMessage();
        Map<String, Object> map = new HashMap<>();
        map.put("name", me.getNickname());
        message.setExtraMap(map);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_inviteYourFriend_invite:
                message.setContent("run");
                c.sendMessage(message, messageSendListener);
                tvText.setText("等待对方响应");
                btnInvite.setEnabled(false);
                break;
            case R.id.btn_inviteYourFriend_cancel:
                finish();
                break;
        }
    }

    @Override
    public void onMessageReceive(List<MessageEvent> list) {
        BmobIMMessage msg = list.get(0).getMessage();
        if ("yes".equals(msg.getContent())) {
            tvText.setText("对方接受了您的邀请");
            sendStart();
        } else if ("no".equals(msg.getContent())) {
            tvText.setText("对方拒绝了您的邀请");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        BmobIM.getInstance().addMessageListHandler(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        BmobIM.getInstance().removeMessageListHandler(this);
    }

    private void sendStart() {
        message = new BmobIMTextMessage();
        Map<String, Object> map = new HashMap<>();
        map.put("name", me.getNickname());
        message.setExtraMap(map);
        message.setContent("start");
        c.sendMessage(message, messageSendListener);
        //启动新的界面
        Intent intent = new Intent(InviteYourFriendActivity.this, StartRunActivity.class);
        intent.putExtra("data", user);
        startActivity(intent);
        finish();

    }

    private MessageSendListener messageSendListener = new MessageSendListener() {
        @Override
        public void done(BmobIMMessage bmobIMMessage, BmobException e) {

        }
    };
}
